package app.esb.common;

import java.io.File;

/**
 * 文件工具类
 * @author JiaJ
 * @date 2017年12月2日
 *
 */
public class FileTools {
	
	/**
	 * 获取文件体积 - 文字信息
	 * @param filePath
	 * @return
	 */
	public static String getFileSize(String filePath){
		
		filePath = filePath.replace("\\\\", "/");
		
		File file = new File(filePath);
		
		double KB = getTotalFileSize(file)/1024.0;
		
		//2,683,169,076
		return NumberTools.toSizeString(KB);
	}
	
	/**
	 * 获取文件体积 - KB数
	 * @param file
	 * @return
	 */
	public static long getTotalFileSize(File file) {
		
        if (file.isFile()){
        	return file.length();
        }
            
        final File[] children = file.listFiles();
        long total = 0;
        if (children != null){
        	for (File child : children){
           	 total += getTotalFileSize(child);
           }
        }
                 
        return total;
	}
	
	/**
	 * 获取相对路径 - 盘符后路径信息
	 * @param location
	 * @return
	 */
	public static String getRelaLocation(String location){
		
		location = location.replaceAll("\\\\", "/");
		
		int index = location.indexOf(":");
		
		if(index > -1 && index <= 2){
			location = location.substring(index+1);
		}
		
		return location;
		
	}
	
}
